/**
 * @file ObservationModel.h
 *
 * Model tabeli obserwacji - nagłówek.
 *
 * @author Zbigniew Siciarz
 * @date 2009
 * @version 2.0.0
 * @since 2.0.0
 */

#include "ObservationModel.h"

#include "dogz/ObservationManager.h"
#include <QString>


ObservationModel::ObservationModel(QObject* parent):
    QSqlTableModel(parent)
{
    setTable("base_observations");
    setSort(Dogz::ObservationManager::Jd, Qt::DescendingOrder);

    setHeaderData(Dogz::ObservationManager::StarName, Qt::Horizontal,
            tr("Nazwa gwiazdy"));
    setHeaderData(Dogz::ObservationManager::Jd, Qt::Horizontal,
            tr("JD"));
    setHeaderData(Dogz::ObservationManager::Magnitude, Qt::Horizontal,
            tr("Jasność"));
    setHeaderData(Dogz::ObservationManager::CompStarName, Qt::Horizontal,
            tr("Gwiazdy porównania"));
    setHeaderData(Dogz::ObservationManager::ChartDate, Qt::Horizontal,
            tr("Mapka"));
    setHeaderData(Dogz::ObservationManager::CommentCodes, Qt::Horizontal,
            tr("Komentarze"));
    setHeaderData(Dogz::ObservationManager::Notes, Qt::Horizontal,
            tr("Uwagi"));
}

QVariant ObservationModel::data(const QModelIndex& item, int role) const
{
    QVariant data = QSqlTableModel::data(item, role);

    if (role == Qt::DisplayRole)
    {
        if (item.column() == Dogz::ObservationManager::Magnitude)
        {
            QModelIndex fainterIndex = createIndex(item.row(),
                    Dogz::ObservationManager::FainterThan);
            bool fainter = QSqlTableModel::data(fainterIndex,
                    Qt::DisplayRole).toBool();
                return (fainter ? "<" : "") +
                QString::number(data.toDouble(), 'f', 2);
        }
    }

    return data;
}
